package tcdemo1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        //TCP协议 接受数据
        //1.创建ServerSocket对象
        ServerSocket serverSocket = new ServerSocket(8888);

        //2.监听客户端连接 说白了就是等待客服端发送数据，连接成功返回一个Socket对象
        Socket socket = serverSocket.accept();
        System.out.println("连接成功");
        //3.读取数据
        InputStream is = socket.getInputStream();
        InputStreamReader fis = new InputStreamReader(is);
        //4.读取数据
        int b;
        while ((b = fis.read()) != -1) {
            System.out.print((char) b);
        }
        //5.关闭资源

        socket.close();;//断开了与客服端连接
        serverSocket.close();//关闭了服务器
    }
}
